package com.cy.filters;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @ClassName ServerBlockExceptionHandler
 * @Description:类描述
 * @Author Tedu
 * @Date 2021/6/2 14:06
 * @Version V1.0
 **/

/*自定义限流异常处理器 默认为DefaultBlockExceptionHandler*/
@Component
public class ServiceBlockExceptionHandler implements BlockExceptionHandler {
    @Override
    public void handle(
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse,
            BlockException e)
            throws Exception {

        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setContentType("text/html; charset=UTF-8");
        PrintWriter out=httpServletResponse.getWriter();
        //out.println("blockException");

        out.println("请稍等片刻...");
        out.flush();

    }
}
//@Component
//public class ServiceBlockExceptionHandler
//        implements BlockExceptionHandler {
//    @Override
//    public void handle(HttpServletRequest request,
//                       HttpServletResponse response,
//                       BlockException e) throws Exception {
//        //response.setStatus(601);
//        //设置响应数据的编码
//        response.setCharacterEncoding("utf-8");
//        //告诉客户端要响应的数据类型以及客户端以什么编码呈现数据
//        response.setContentType("text/html;charset=utf-8");
//        PrintWriter pw=response.getWriter();
//        Map<String,Object> map=new HashMap<>();
//        if(e instanceof DegradeException){//降级、熔断
//            map.put("status",601);
//            map.put("message", "服务被熔断了!");
//        }else if(e instanceof FlowException){
//            map.put("status",602);
//            map.put("message", "服务被限流了!");
//        }else{
//            map.put("status",603);
//            map.put("message",
//                    "Blocked by Sentinel (flow limiting)");
//        }
//        //将map对象转换为json格式字符串
//        String jsonStr=new ObjectMapper().writeValueAsString(map);
//        pw.println(jsonStr);
//        pw.flush();
//    }
//}
